<?php

namespace loggedin\model;

require_once("/loggedin/model/Question.php");

class GetQuestions {
	/**
	 * Funktion som returnerar antalet frågor i databasen.
	 * Om det inte finns några så returneras 0.
	 * @param  mysqli $mysqli
	 * @return int
	 * @throws Exception om något går fel i databasen.
	 */
	public function getQuestionCount(\mysqli $mysqli) {
		$sqlQ = "SELECT COUNT(*) FROM `Questions`";

		$stmt = $mysqli->prepare($sqlQ);
		if ($stmt == false) {
			throw new \Exception("Prepare of sql question failed " . $mysqli->error);
		}
		$res = $stmt->execute();
		if($res == false) {
			throw new \Exception("Database execute failed " . $stmt->error);
		}
		$res = $stmt->bind_result($QuestionCount);
		if ($res == false) {
			throw new \Exception("Bind result failed " . $stmt->error);
		}
		if($stmt->fetch()) {
			return $QuestionCount;
		}
		return 0;
	}

	/**
	 * @param  mysqli $mysqli
	 * @param  string $username
	 * @return \loggedin\model\Question
	 * @throws Exception om något går fel i databasen
	 */
	public function getAllQuestions(\mysqli $mysqli, $username) {
		$sqlQ = "SELECT * FROM `Questions` WHERE `Username` = ?";

		$stmt = $mysqli->prepare($sqlQ);
		if ($stmt == false) {
			throw new \Exception("Prepare of sql question failed " . $mysqli->error);
		}
		$res = $stmt->bind_param("s", $username);
		if ($res == false) {
			throw new \Exception("Bind param failed " . $stmt->error);
		}
		$res = $stmt->execute();
		if($res == false) {
			throw new \Exception("Database execute failed " . $stmt->error);
		}
		$res = $stmt->bind_result($QuestionID, $Username, $Question, 
								  $RightAnswer, $WrongAnswer1, 
								  $WrongAnswer2, $WrongAnswer3);
		if ($res == false) {
			throw new \Exception("Bind result failed " . $stmt->error);
		}
		$questions = array();
		while ($stmt->fetch()) {
			$questions[] = new Question($QuestionID, $Question, 
								  		$RightAnswer, $WrongAnswer1, 
								  		$WrongAnswer2, $WrongAnswer3);
		}
		return $questions;
	}

	/**
	 * @param  int $questionID
	 * @param  mysqli $mysqli
	 * @param  string $username
	 * @return \loggedin\model\Question
	 * @throws Exception om något går fel i databasen.
	 */
	public function getQuestion($questionID, \mysqli $mysqli, $username = "") {
		$sqlQ = "SELECT * FROM `Questions` WHERE `QuestionID` = ?";
		if (!empty($username)) {
			$sqlQ = "SELECT * FROM `Questions` WHERE `Username` = ? AND `QuestionID` = ?";
		}
		$stmt = $mysqli->prepare($sqlQ);
		if ($stmt == false) {
			throw new \Exception("Prepare of sql question failed " . $mysqli->error);
		}
		if(!empty($username)) {
			$res = $stmt->bind_param("ss", $username, $questionID);
			if ($res == false) {
				throw new \Exception("Bind param failed " . $stmt->error);
			}
		} else {
			$res = $stmt->bind_param("s", $questionID);
			if ($res == false) {
				throw new \Exception("Bind param failed " . $stmt->error);
			}
		}
		$res = $stmt->execute();
		if($res == false) {
			throw new \Exception("Database execute failed " . $stmt->error);
		}
		$res = $stmt->bind_result($QuestionID, $Username, $Question, 
								  $RightAnswer, $WrongAnswer1, 
								  $WrongAnswer2, $WrongAnswer3);
		if ($res == false) {
			throw new \Exception("Bind result failed " . $stmt->error);
		}
		if($stmt->fetch()) {
			return new Question($QuestionID, $Question,
								$RightAnswer, $WrongAnswer1,
								$WrongAnswer2, $WrongAnswer3);
		}
		throw new \Exception("Användare har inte tillgång till frågan");
	}
}